package com.test.test.utils.aspect;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/*@Component
@Aspect*/
public class HttpAspect {
	// 监听controller 下面的所有类所有方法
	 private static final String EXECUTION_COM_TEST_TEST_CONTROLLER =
	 "execution(* com.test.test.controller.*.*(..))";

	Logger LOG = LoggerFactory.getLogger(HttpAspect.class);

	// 监听controller 下面的所有类所有方法
	@Pointcut(EXECUTION_COM_TEST_TEST_CONTROLLER)
	public void log() {
	}

	// @Before(EXECUTION_COM_TEST_TEST_CONTROLLER)
	@Before("log()")
	public void doefore(JoinPoint joinPoint) {
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		HttpServletRequest request = servletRequestAttributes.getRequest();
		LOG.info("url = {}", request.getRequestURI());
		LOG.info("month = {}", request.getMethod());
		LOG.info("ip = {}", request.getRemoteAddr());
		LOG.info("class_month = {}",
				joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
		LOG.info("args = {}", joinPoint.getArgs());
	}

	// @After(EXECUTION_COM_TEST_TEST_CONTROLLER)
	@After("log()")
	void doAfert() {
		LOG.info("13333333");
		// System.out.println("133333");
	}

	@AfterReturning(returning = "object", pointcut = "log()")
	void doAfterReturning(Object object) {
		LOG.info("object = {}", object);
	}

}
